﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using QuizApp.Web;

namespace QuizApp.Views
{
    public partial class Materia : Page
    {
        public Materia()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void materiaDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void addMateriaButton_Click(object sender, RoutedEventArgs e)
        {
            

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

        }

        private void materiaDomainDataSource_LoadedData_1(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void materiaDomainDataSource1_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void MateriaButton_Click(object sender, RoutedEventArgs e)
        {
            //var ctx = new QuizDomainContext();
            QuizDomainContext ctx = (QuizDomainContext)materiaDomainDataSource.DomainContext;
            Web.Materia tmp = new Web.Materia() { Nome = textBox1.Text };
            ctx.Materias.Add(tmp);
            ctx.SubmitChanges();
        }

        private void TopicoButton_Click(object sender, RoutedEventArgs e)
        {
            if (materiaDataGrid.SelectedItem != null)
            {
                QuizDomainContext ctx = (QuizDomainContext)materiaDomainDataSource.DomainContext;
                Web.Topico tmp = new Web.Topico() { Nome = textBox1.Text, idMateria = (materiaDataGrid.SelectedItem as Web.Materia).idMateria };
                ctx.Topicos.Add(tmp);
                ctx.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Selecione uma máteria");
            }
        }

        private void SubTopicoButton_Click(object sender, RoutedEventArgs e)
        {
            if (topicoDataGrid.SelectedItem != null)
            {
                QuizDomainContext ctx = (QuizDomainContext)materiaDomainDataSource.DomainContext;
                Web.SubTopico tmp = new Web.SubTopico() { Nome = textBox1.Text, idMateria = (materiaDataGrid.SelectedItem as Web.Materia).idMateria, idTopico = (topicoDataGrid.SelectedItem as Web.Topico).idTopico };
                ctx.SubTopicos.Add(tmp);
                ctx.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Selecione uma Topico");
            }
        }

        private void materiaDomainDataSource_LoadedData_2(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

    }
}
